package com.example.myapplication2;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.POJO.Shop.ShoppingCart;
import com.example.myapplication2.dataBase.ShopHelper;

import java.util.List;

public class ShopCarActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView title;
    private ShopHelper shopHelper;
    private LinearLayout linearLayout;
    private TextView title_count;
    private LinearLayout shop_car_bottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_car);
        title = findViewById(R.id.shopTitle);
        title_count = findViewById(R.id.title_count);
        title.setText("购物车");
        shop_car_bottom = findViewById(R.id.shop_car_bottom);
        findViewById(R.id.chop_car_submit).setOnClickListener(this);
        findViewById(R.id.chop_car_clear).setOnClickListener(this);
        findViewById(R.id.title_back).setOnClickListener(this);
        linearLayout = findViewById(R.id.cardList);
        //查询数据库里面的购物车数据，并展示
        shopHelper = ShopHelper.getInstance(this);

        showCar();
    }

    private void showCar() {
        boolean flag=true;
        //移除子视图
        linearLayout.removeAllViews();
        //查询数据库是否为空
        List<ShoppingCart> shoppingCarts = shopHelper.ShoppingCartList();
        if (shoppingCarts.size()==0){
            //购物车为空
            View inflate = LayoutInflater.from(this).inflate(R.layout.card_null, null);
            linearLayout.addView(inflate);
            shop_car_bottom.setVisibility(LinearLayout.GONE);
        }else {
            //购物车非空
            for (ShoppingCart shoppingCart : shoppingCarts) {
                View inflate = LayoutInflater.from(this).inflate(R.layout.card_shop_car, null);
                TextView shopCar_good_img = inflate.findViewById(R.id.shopCar_good_img);
                shopCar_good_img.setText(shoppingCart.getImg());
                TextView card_good_name = inflate.findViewById(R.id.card_good_name);
                card_good_name.setText(shoppingCart.getGoodName());
                TextView card_good_introduce = inflate.findViewById(R.id.card_good_introduce);
                card_good_introduce.setText(shoppingCart.getIntroduce());
                TextView card_good_price = inflate.findViewById(R.id.card_good_price);
                card_good_price.setText(shoppingCart.getPrice().toString());
                TextView card_good_count = inflate.findViewById(R.id.card_good_count);
                card_good_count.setText(shoppingCart.getCount().toString());
                TextView card_good_total_Price = inflate.findViewById(R.id.card_good_total_Price);
                card_good_total_Price.setText(shoppingCart.getTotalPrice().toString());
                if (flag){
                    inflate.setBackgroundColor(Color.WHITE);
                    flag= false;
                }else {
                    inflate.setBackgroundColor(Color.GRAY);
                    flag= true;
                }
                inflate.setOnLongClickListener(view -> {
                    alertDialog(shoppingCart);
                    return true;
                });
                linearLayout.addView(inflate);
            }
            //可见
            shop_car_bottom.setVisibility(LinearLayout.VISIBLE);
        }
        //更新标题的计数
        Long count = shopHelper.Count();
        title_count.setText(count.toString());
    }

    private void alertDialog(ShoppingCart shoppingCart) {
        //构造弹窗对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置标题
        builder.setTitle("弹窗标题:是否删除改商品"+shoppingCart.getGoodName());
        //设置消息内容
        builder.setMessage("弹窗消息内容:确定删除吗????");
        //设置确定执行对象
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //提示消息
                Toast.makeText(ShopCarActivity.this,"确定了",Toast.LENGTH_SHORT).show();
                shopHelper.deleteByGoodId(shoppingCart.getId());
                showCar();
            }
        });
        //设置取消执行对象
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //提示消息
                Toast.makeText(ShopCarActivity.this,"取消了",Toast.LENGTH_SHORT).show();
            }
        });
        //创建,显示
        builder.create().show();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.chop_car_submit:
                Toast.makeText(this,"购物车提交成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.chop_car_clear:
                Toast.makeText(this,"购物车清空成功",Toast.LENGTH_SHORT).show();
                clear();
                break;
            case R.id.title_back:
                finish();
                break;
        }
    }

    private void clear() {
        //移除子视图
        linearLayout.removeAllViews();
        View inflate = LayoutInflater.from(this).inflate(R.layout.card_null, null);
        linearLayout.addView(inflate);
        shopHelper.empty();
        title_count.setText("0");
    }

    @Override
    protected void onResume() {
        super.onResume();
        showCar();
    }
}